﻿<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" %>

<%@ Import Namespace="System.Text.RegularExpressions" %>
<%@ Import Namespace="UserManagementWCFService" %>
<%@ Import Namespace="UserManagementService" %>

<script runat="server">

    protected void createUserButton_Click(object sender, EventArgs e)
    {
        EncryptionService.ServiceClient encryptionClient = new EncryptionService.ServiceClient();
        errorMessages.InnerText = "";
        if (completeInputs())
        {
            if (availableUsername(usernameInput.Value))
            {
                User user = new User();
                user.Username = usernameInput.Value;
                user.Email = emailInput.Value;
                user.Password = encryptionClient.Encrypt(passwordInput.Value);
                user.Identifier = Guid.NewGuid();
                addUser(user);
                Response.Redirect(ConfigurationManager.AppSettings["BasePath"] + "Login.aspx");
            }
        }

        encryptionClient.Close();
    }

    public void addUser(User user)
    {
        if (user.Identifier == null)
        {
            user.Identifier = Guid.NewGuid();
        }
        UserManagementServiceClient client =
            new UserManagementServiceClient();
        client.addUser(user);
        client.Close();
    }

    public bool availableUsername(string username)
    {
        UserManagementServiceClient client =
            new UserManagementServiceClient();
        bool available = client.availableUserName(username);
        client.Close();
        if (!available)
        {
            errorMessages.InnerText = "Unavailable Username.";
        }
        return available;
    }

    private bool completeInputs()
    {
        StringBuilder builder = new StringBuilder();
        bool complete = true;
        builder.Append("Please enter valid");
        if (usernameInput.Value.Equals("") || usernameInput.Value.Equals("Username"))
        {
            complete = false;
            builder.Append(" username,");
        }
        if (emailInput.Value.Equals("") || !Regex.Match(emailInput.Value, "^\\w*\\@\\w*\\.[a-zA-z]{3}").Success)
        {
            complete = false;
            builder.Append(" email address,");
        }
        if (passwordInput.Value.Equals(""))
        {
            complete = false;
            builder.Append(" password,");
        }
        if (!passwordValidation.Value.Equals(passwordInput.Value))
        {
            complete = false;
            builder.Append(" matching passwords ");
        }
        builder.Remove(builder.Length - 1, 1);
        builder.Append(".");
        if (!complete)
        {
            errorMessages.InnerText = builder.ToString();
        }
        return complete;
    }

</script>

<asp:Content ID="Content1" ContentPlaceHolderID="Body" runat="Server">
    <script>
        window.onload =
            function () {
                document.getElementById("logoutLink").innerHTML = "";
            }
    </script>
    <table>
        <tr>
            <td>
                <input
                    type="text"
                    id="usernameInput"
                    size="40"
                    value="Username"
                    onfocus="if(this.value=='Username')this.value='';"
                    onblur="if(this.value=='')this.value='Username';"
                    runat="server" />
            </td>
        </tr>
        <tr>
            <td>
                <input
                    type="text"
                    id="emailInput"
                    size="40"
                    value="Email"
                    onfocus="if(this.value=='Email')this.value=''"
                    onblur="if(this.value=='')this.value='Email'"
                    runat="server" />
            </td>
        </tr>
        <tr>
            <td></td>
        </tr>
        <tr>
            <td>Password</td>
        </tr>
        <tr>
            <td>
                <input
                    type="password"
                    id="passwordInput"
                    size="40"
                    runat="server" />
            </td>
        </tr>
        <tr>
            <td>Validate Password</td>
        </tr>
        <tr>
            <td>
                <input
                    type="password"
                    id="passwordValidation"
                    size="40"
                    runat="server" />
            </td>
        </tr>
        <tr>
            <td>
                <p id="errorMessages" runat="server" style="color: red" />
                &nbsp;</td>
        </tr>
    </table>
    <asp:Button ID="createUserButton" runat="server" Text="Create User" OnClick="createUserButton_Click" />
</asp:Content>

